package com.cy.detectsys.controller.oss;

import com.cy.detectsys.model.OssFile;
import com.cy.detectsys.util.MinioTemplate;
import com.cy.detectsys.util.R;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import static com.cy.detectsys.util.FileOperateUtil.multipartFileToFile;

@RestController
@RequestMapping("file")
public class FileController {

    @Resource
    private MinioTemplate minioTemplate;

    @RequestMapping("upload")
    public R upload(@RequestParam("file") MultipartFile file) {
        OssFile ossFile = minioTemplate.upLoadFile("result", multipartFileToFile(file));

//        OssFile ossFile = minioTemplate.upLoadFile(" result", file.getName(),file);
        System.out.println(ossFile);
        return R.ok(ossFile);
    }




    //@RequestMapping("download")
    //public void upload(@RequestParam("file") String fileName, HttpServletResponse response) {
    //    minioTemplate.downloadFile(response, fileName, "/demo/20240506/file_1714987521718.sql");
    //}


    @RequestMapping("path/download")
    public void upload(@RequestParam("path") String path, HttpServletResponse response) {
        minioTemplate.downloadFile(response, path);
    }
}
